<?php
namespace Upload\Driver;

use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;
use OSS\OssClient;
use Upload\UploadInterface;
use Yaf\Exception;
use Config;

class AliOss implements UploadInterface
{
    public OssClient $ossClient;
    /**
     * @var array
     */
    private array $config;

    /**
     * 上传错误信息
     * @var string
     */
    private string $error = '';

    public function __construct()
    {
        $this->config = Config::get('alioss');
        $this->ossClient = new OssClient($this->config['access_key_id'],
            $this->config['access_key_secret'],
            $this->config['end_point']
        );
    }

    /**
     * 获取oss对象
     * @return OssClient
     */
    public function getOssObject(): OssClient
    {
        return $this->ossClient;
    }
    /**
     * 创建存储空间
     *
     * @param $bucket
     * @return string
     * @throws RequestCore_Exception|Exception
     */
    public function createBucket($bucket): string
    {
        try {
            $this->ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ); // 设置存储空间的权限为公共读，默认是私有读写。
        } catch (OssException $e) {
            $this->error = $e->getMessage();
            throw new Exception($e->getMessage());
        }

        return "创建存储空间{$bucket}成功";
    }

    /**
     * 上传文件
     * @param $filePath
     * @param $fileName
     * @param $bucket
     * @return array|string
     */
    public function uploadFiles($filePath, $fileName = null, $bucket = null): array|string
    {
        try {
            $bucket = $bucket ?? $this->config['bucket'];
            $explode = explode('/', $filePath);
            $fileName = $fileName ?? end($explode);
            // 上传
            $result = $this->ossClient->uploadFile($bucket, $fileName, $filePath);
            //删除源文件
            @unlink($filePath);
            return $result['info']['url'];
        } catch (OssException|RequestCore_Exception $e) {
            $this->error = $e->getMessage();
            if (isset($filePath) && file_exists($filePath)) {
                @unlink($filePath);
            }
        }
        return [];
    }

    /**
     * 删除文件
     * @param $fileName
     * @param $bucket
     * @return void
     * @throws Exception
     */
    public function deleteFile($fileName = null, $bucket = null): void
    {
        try {
            $bucket = $bucket ?? $this->config['bucket'];
            $this->ossClient->deleteObject($bucket, $fileName);
        } catch (OssException|RequestCore_Exception $e) {
            $this->error = $e->getMessage();
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 多上传文件
     * @param array|string $files
     * @param $fileName
     * @param $bucket
     * @return bool|array
     * @throws Exception
     */
    public function upload(array|string $files, $fileName = null, $bucket = null): bool|array
    {
        $result = [];
        try {
            $bucket = $bucket ?? $this->config['bucket'];
            if (is_array($files)) {
                foreach ($files as $file) {
                    $explode = explode('/', $file);
                    $fileName = $fileName ?? end($explode);
                    // 上传
                    $result[] = $this->ossClient->uploadFile($bucket, $fileName, $file);
                }
            }else {
                $explode = explode('/', $files);
                $fileName = $fileName ?? end($explode);
                // 上传
                $result[] = $this->ossClient->uploadFile($bucket, $fileName, $files);
            }
        } catch (OssException|RequestCore_Exception $e) {
            $this->error = $e->getMessage();
            throw new Exception($e->getMessage());
        }
        return $result;
    }

    public function getError(): string
    {
        // TODO: Implement getError() method.
        return $this->error;
    }
}
